package cn.xuyang520.tyim.sys.controller;

import java.text.MessageFormat;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import cn.xuyang520.tyim.sys.core.TySysAjaxException;
import cn.xuyang520.tyim.sys.core.TySysAjaxResult;
import cn.xuyang520.tyim.sys.domain.TySysUser;

/**
 * @author wangbingya
 * @ClassName: BaseController
 * @Description:基类Controller
 * @date 2017年4月2日 下午4:03:30
 */
public abstract class TySysBaseController {
    public final Logger logger = LoggerFactory.getLogger(this.getClass());
    public static final String TEXT_UTF8 = "text/html;charset=UTF-8";
    public static final String JSON_UTF8 = "application/json;charset=UTF-8";
    public static final String XML_UTF8 = "application/xml;charset=UTF-8";

    public static final String LIST = "list";
    public static final String VIEW = "view";
    public static final String ADD = "add";
    public static final String SAVE = "save";
    public static final String EDIT = "edit";
    public static final String UPDATE = "update";
    public static final String DELETE = "delete";
    public static final String PAGE = "page";

    /**
     * 得到request对象
     */
    public HttpServletRequest getRequest() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        return request;
    }

    public String redirect(String format, Object... arguments) {
        return new StringBuffer("redirect:").append(MessageFormat.format(format, arguments)).toString();
    }

    /**
     * 返回操作失败json对象
     */
    public Object failed() {
        return failed("失败");
    }

    /**
     * 返回操作失败json对象，msg为返回的消息文本
     */
    public Object failed(String msg) {
        TySysAjaxResult r = new TySysAjaxResult();
        r.setSuccess(false);
        r.setMsg(msg);
        return r;
    }
    private static TySysAjaxResult FAILES_AJAX_RESULT = new TySysAjaxResult();
    private static TySysAjaxResult SUCCSEE_AJAX_RESULT = new TySysAjaxResult();
    static {
        FAILES_AJAX_RESULT.setSuccess(false);
        FAILES_AJAX_RESULT.setMsg("失败");
    }
    /**
     * 返回操作成功json对象
     */
    public Object success() {
        return SUCCSEE_AJAX_RESULT;
    }

    /**
     * 返回操作成功json对象，msg为返回的消息文本
     */
    public Object success(String msg) {
        TySysAjaxResult r = new TySysAjaxResult();
        r.setMsg(msg);
        return r;
    }

    /**
     * 判断当前登陆用户是否具有任意权限，若没有，则抛出异常
     */
    public Subject checkPermissions(String... permitted) {
        return checkPermissions(permitted);
    }

    /**
     * 判断当前登陆用户是否具有全部权限，若没有，则抛出异常
     */
    public Subject checkPermissionsAll(String... permitted) {
        Subject subject = getSubject();
        if (subject.isPermittedAll(permitted))
            throw new TySysAjaxException("无此操作权限");
        return subject;
    }

    /**
     * 获取Subject
     */
    public Subject getSubject() {
        return SecurityUtils.getSubject();
    }

    /**
     * 获取当前登录用户对象
     */
    public TySysUser getLoginUser() {
        return (TySysUser) getSubject().getSession().getAttribute("user");
    }

}
